package com.vhall.component.framework.feignclient.core;

import com.vhall.component.framework.feignclient.handler.RenamingServletModelAttributeMethodProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * Feign代理mvc
 *
 * @author Administrator
 */
public class FeignProxyMvcConfiguration {

    @Autowired
    private RequestMappingHandlerAdapter adapter;

    @PostConstruct
    public void modifyArgumentResolvers() {
        List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
        resolvers.addAll(adapter.getArgumentResolvers());
        for (int i = resolvers.size() - 1; i > 0; i--) {
            HandlerMethodArgumentResolver resolver = resolvers.get(i);
            if (resolver instanceof ServletModelAttributeMethodProcessor) {
                resolvers.set(i, new RenamingServletModelAttributeMethodProcessor(adapter));
                break;
            }
        }
        adapter.setArgumentResolvers(resolvers);
    }
}
